﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;

namespace REVISIONMAX30
{
    public class Functions
    {
        public static REVISIONMAX30.Frame.FrResultType ExecuteBatch(string pBatchFilePath, bool pWaitForExit)
        {
            var tFrResult = new REVISIONMAX30.Frame.FrResultType();

            var tProcess = new Process();
            try
            {
                tProcess.StartInfo.ErrorDialog = false;
                tProcess.StartInfo.UseShellExecute = false;
                tProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                tProcess.StartInfo.FileName = pBatchFilePath;
                tProcess.StartInfo.RedirectStandardError = true;
                tProcess.StartInfo.RedirectStandardOutput = true;
                tProcess.StartInfo.StandardErrorEncoding = Encoding.Default;
                tProcess.StartInfo.StandardOutputEncoding = Encoding.Default;
                tProcess.Start();

                if (pWaitForExit)
                {
                    tProcess.WaitForExit();
                }
            }
            catch (Exception exc)
            {
                if (tProcess != null)
                {
                    tProcess.Kill();
                }

                tFrResult.AddResult(exc);
            }

            var tStandardError = tProcess.StandardError;
            var tStandardOutput = tProcess.StandardOutput;

            var tStandardErrorBuilder = new StringBuilder();

            while (!tStandardError.EndOfStream)
            {
                var tString = tStandardError.ReadLine().Trim();
                if (tString.IsEmpty())
                {
                    tStandardErrorBuilder.AppendLine(tString);
                }
            }

            tFrResult.AddResult(tStandardErrorBuilder.ToString(), REVISIONMAX30.Frame.FrResultSeverityType.Error);

            var tStandardOutputBuilder = new StringBuilder();
            while (!tStandardOutput.EndOfStream)
            {
                var tString = tStandardOutput.ReadLine().Trim();
                if (tString.IsEmpty())
                {
                    tStandardOutputBuilder.AppendLine(tString);
                }
            }

            tFrResult.AddResult(tStandardOutputBuilder.ToString(), REVISIONMAX30.Frame.FrResultSeverityType.Info);

            return tFrResult;
        }

        public static Frame.FrResultType CopyDirectoryWithFiles(string pDirectory, bool pRecursive, string pFilePattern, bool pOverwrite )
        {
            return CopyDirectoryWithFiles(pDirectory, pRecursive, pFilePattern, pOverwrite, "");
        }

        public static Frame.FrResultType CopyDirectoryWithFiles(string pDirectory, bool pRecursive, string pFilePattern, bool pOverwrite, string pLocalRootDirectory)
        {
            var tFrResult = new Frame.FrResultType();

            var tDirectory = new System.IO.DirectoryInfo(pDirectory);
            var tLocalRootDirectory = "";

            if (pLocalRootDirectory.IsEmpty())
            {
                tLocalRootDirectory = string.Format(@".\SQLS\{0}\", tDirectory.Name);
            }
            else
            {
                tLocalRootDirectory = string.Format(@"{1}{0}\", pLocalRootDirectory, tDirectory.Name);
            }

            tFrResult.AddResult(string.Format("New Directory {0}", tLocalRootDirectory), Frame.FrResultSeverityType.Info);

            var tFiles = tDirectory.GetFiles(pFilePattern);

            if (tFiles.Length > 0 && !System.IO.Directory.Exists(tLocalRootDirectory))
            {
                System.IO.Directory.CreateDirectory(tLocalRootDirectory);
                tFrResult.AddResult(string.Format("Created New Directory {0}", tLocalRootDirectory), Frame.FrResultSeverityType.Info);
            }

            foreach (var tFile in tFiles)
            {
                var tNewFileName = string.Format("{0}{1}", tLocalRootDirectory, tFile.Name);

                if (pOverwrite || !System.IO.File.Exists(tNewFileName))
                {
                    tFile.CopyTo(tNewFileName, pOverwrite);
                    tFrResult.AddResult(string.Format("New File {0}", tNewFileName), Frame.FrResultSeverityType.Info);
                }
            }

            if (pRecursive)
            {
                var tSubDirectories = tDirectory.GetDirectories();

                foreach (var tSubDirectory in tSubDirectories)
                {
                    tFrResult.AddResult(CopyDirectoryWithFiles(tSubDirectory.FullName, pRecursive, pFilePattern, pOverwrite, tLocalRootDirectory));
                }
            }

            return tFrResult;
        }
    }
}
